package com.ts.crawler.selenium;

import com.ts.crawler.pipeline.FlightInfoPipeline;
import com.ts.crawler.scheduler.FlightInfoScheduler;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class FlightCtripProcesser implements PageProcessor {

//    private RemoteWebDriver driver;
//
//    private static final String targetUrl ="https://flights.ctrip.com/international/search/domestic";

    private Site site = Site.me().setSleepTime(1000).setTimeOut(10000).setCharset("utf-8")
            .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");

    public void process(Page page) {
        Html html = page.getHtml();

        List<Selectable> nodes = html.css(".flight-list > span > div").nodes();
//        System.out.println(nodes.toString());
        System.out.println(nodes.size());
        int count=0;
        for (Selectable node : nodes) {
            Map<String,String> map = new HashMap();
            StringBuilder sb = new StringBuilder();
            String airlineName = node.$(".flight-row").xpath("///div[@class='flight-airline']/div[@class='airline-name']/span/text()").toString();
            if(airlineName == null){
                continue;
            }

            String departTime = node.$(".flight-row").xpath("///div[@class='flight-detail']/div[@class='depart-box']/div[@class='time']/text()").toString();
            String arriveTime = node.$(".flight-row").xpath("///div[@class='flight-detail']/div[@class='arrive-box']/div[@class='time']/text()").toString();
//            List<Selectable> planes = node.$(".flight-row").xpath("///div[@class='flight-airline']/div[@class='plane']/div[@class='plane']").nodes();
//            for(Selectable p : planes){
//                String plane = p.xpath("///span[@class='plane-No']/span[@class=' ']/text()").toString();
//                System.out.println("机型"+plane);
//                System.out.println(p);
//            }

            String departAirPort = node.$(".flight-row").xpath("///div[@class='flight-detail']/div[@class='depart-box']/div[@class='airport']/span/text()").toString();
            String arriveAirPort = node.$(".flight-row").xpath("///div[@class='flight-detail']/div[@class='arrive-box']/div[@class='airport']/span/text()").toString();
            String totalTime = node.$(".flight-row").xpath("///div[@class='flight-consume']/text()").toString();
            List<Selectable> cabins = node.$(".flight-seats").xpath("///div[@class='seat-row']").nodes();
//            System.out.println(cabins.size());
//            System.out.println(cabins);
            int i = 1;
            for(Selectable c : cabins){
                String name = c.$(".seat-type").xpath("///span/text()").toString();
                String price = c.$(".seat-operate").xpath("//div[@class='price']").regex("\\>[0-9]+").toString();
                price = price.substring(1);
                sb.append(name+price);
//                System.out.println("舱位：" + name);
//                System.out.println("价格：" + price);

                map.put("舱位"+ i,sb.toString());
                ++i;
            }
            i=1;

//            System.out.println("航班：" + airlineName);
//            System.out.println("起飞时间：" + departTime);
//            System.out.println("到达时间：" + arriveTime);
//            System.out.println("起飞机场：" + departAirPort);
//            System.out.println("到达机场：" + arriveAirPort);
//            System.out.println("飞行时间：" + totalTime);
//            System.out.println("----------------------");

            map.put("flightWay",airlineName);
            map.put("startDate",departTime);
            map.put("endDate",arriveTime);
            map.put("startCity",departAirPort);
            map.put("endCity",arriveAirPort);
            //map.put("飞行时间",totalTime);
            ++count;
            page.putField("result"+count,map);

        }

    }

    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
//        Map<String,String> params =new HashMap<String, String>();
//        //公共参数
//        String startCity = "长沙(CSX)";
//        String destCity = "北京(BJS)";
//        String startDate = "2021-10-28";
//        String endDate ="2021-10-30";
//        String withChild ="true";
//        String withInfant ="true";
//        //Y_S C_F
//        String cabin ="C_F";
//        //国内/国际机票
//        String flightWay ="S";
//        //单程 往返
//        String flightType ="D";
//
//        params.put("startCity",startCity);
//        params.put("destCity",destCity);
//        params.put("startDate",startDate);
//        params.put("endDate",endDate);
//        params.put("withChild",withChild);
//        params.put("withInfant",withInfant);
//        params.put("cabin",cabin);
//        params.put("flightWay",flightWay);
//        params.put("flightType",flightType);
        LinkedList<Request> urlList = new LinkedList<Request>();
        String []urls = {"https://flights.ctrip.com/online/list/oneway-CSX-BJS?_=1&depdate=2021-10-28"
                ,"https://flights.ctrip.com/online/list/oneway-CSX-BJS?_=1&depdate=2021-10-29"
                ,"https://flights.ctrip.com/online/list/oneway-CSX-BJS?_=1&depdate=2021-10-30"
                ,"https://flights.ctrip.com/online/list/oneway-CSX-BJS?_=1&depdate=2021-10-31"
                ,"https://flights.ctrip.com/online/list/oneway-CSX-BJS?_=1&depdate=2021-11-01"
                ,"https://flights.ctrip.com/online/list/oneway-CSX-BJS?_=1&depdate=2021-11-02"
                ,"https://flights.ctrip.com/online/list/oneway-CSX-BJS?_=1&depdate=2021-11-03"
                ,"https://flights.ctrip.com/online/list/oneway-CSX-BJS?_=1&depdate=2021-11-04"
                ,"https://flights.ctrip.com/online/list/oneway-CSX-BJS?_=1&depdate=2021-11-05"
                ,"https://flights.ctrip.com/online/list/oneway-CSX-BJS?_=1&depdate=2021-11-06"
        };
        for(int i = 0;i<10;i++){
            urlList.add(new Request(urls[i]));
        }


        Spider.create(new FlightCtripProcesser())
                .addPipeline(new FlightInfoPipeline("C:\\Downloads\\Programming\\Java_Code\\TS_Crawler\\src\\main\\java\\com\\ts\\crawler\\data\\"))
//                .addPipeline(new FilePipeline())
                .addPipeline(new ConsolePipeline())
                .setScheduler(new FlightInfoScheduler(urlList))
                .setDownloader(new FlightCtripDownLoader())
                .addUrl("https://flights.ctrip.com/international/search/domestic")
                .thread(1)
                .run();
    }
}
